package util

import (
	"fmt"
	"strconv"
	"strings"
)

// Define a struct to hold the parsed data.
type Classification struct {
	TypeID string
	Value  int
}

// ParseLType takes a string in the format "typeID,value" and returns a slice of Classification structs.
func ParseLType(input string) []Classification {
	var classifications []Classification
	pairs := strings.Split(input, "|")

	for _, pair := range pairs {
		items := strings.Split(pair, ",")
		if len(items) == 2 {
			classification := Classification{
				TypeID: items[0],
				Value:  atoi(items[1]), // Assuming items[1] is an integer
			}
			classifications = append(classifications, classification)
		}
	}

	return classifications
}

// atoi is a simple function to convert string to integer.
// It assumes that the input is a valid integer string.
func atoi(s string) int {
	i, err := strconv.Atoi(s)
	if err != nil {
		// Handle error if necessary
		fmt.Println("Error converting string to integer:", err)
		return 0
	}
	return i
}

// func main() {
// 	ltype := "01,2|02,10"

// 	classifications := ParseLType(ltype)

// 	for _, c := range classifications {
// 		fmt.Printf("TypeID: %s, Value: %d\n", c.TypeID, c.Value)
// 	}
// }
